package com.zhengb.flink.study.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * Created by zhengbo on 2019/11/29.
 */
public class BatchWordCountApp {

    public static void main(String[] args) throws Exception {

        //创建执行环境
        ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();

        String fileInput = "file:///d:/etc/word.txt";
        //输入数据源
        DataSource<String> dataSource = environment.readTextFile(fileInput);

        dataSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> collector) throws Exception {

                String[] tokens = value.toLowerCase().split("\t");

                for (String token : tokens) {
                    if (token.length() > 0) {
                        collector.collect(new Tuple2<>(token, 1));
                    }
                }
            }

        }).groupBy(0).sum(1).print();



    }
}
